package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.ShoppingCartService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @GetMapping("/list")
    public R list(HttpSession session){
        // 得到当前登录的用户
        Long userId = (Long) session.getAttribute("user");
        // 根据用户的id查看购物车
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(userId);
        return R.success(shoppingCartList);
    }

    @DeleteMapping("/clean")
    public R clean(HttpSession session){
        // 得到当前登录的用户
        Long userId = (Long) session.getAttribute("user");
        // 根据用户的id查看购物车
        shoppingCartService.clean(userId);
        return R.success("成功删除购物车！");
    }



    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart, HttpSession session){
        // 得到当前登录的用户
        Long userId = (Long) session.getAttribute("user");
        // 补全购物项
        shoppingCart.setUserId(userId);
        // 交给service
        shoppingCart = shoppingCartService.add(shoppingCart);
        return R.success(shoppingCart);
    }
}
